{% extends 'home.html' %}
{% block content-title %}
    <title>修改书籍</title>
{% endblock %}
{% block content_main %}
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">修改图书</h3>
        </div>
        <div class="panel-body">
            <form class="form-horizontal" method="post" action="" id="form_edit">
                <div class="form-group">
                    <label for="InputName" class="col-sm-2 control-label">书名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="InputName" name="title"
                               value="{{ book_obj.name }}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputPrice" class="col-sm-2 control-label">价格</label>
                    <div class="col-sm-10">
                        <div class="col-sm-12 input-group">
                            <input type="text" class="form-control" pattern="[0-9]*[.]?[0-9]+" id="InputPrice"
                                   name="price"
                                   value="{{ book_obj.price }}">
                            <div class="input-group-addon">￥</div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputImage" class="col-sm-2 control-label">书籍封面</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="InputImage" name="image"
                               value="{{ book_obj.image }}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputDetail" class="col-sm-2 control-label">书籍详细网址</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="InputDetail" name="detail_url"
                               value="{{ book_obj.url }}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputDesc" class="col-sm-2 control-label">书籍简介</label>
                    <div class="col-sm-10">
                        <textarea name="desc" id="InputDesc" class="form-control">{{ book_obj.desc }}</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputWordCount" class="col-sm-2 control-label">书籍字数</label>
                    <div class="col-sm-10">
                        <div class="col-sm-12 input-group">
                            <input type="text" class="form-control" pattern="[0-9]*[.]?[0-9]+" id="InputWordCount"
                                   name="word_count" value="{{ book_obj.word_count }}">
                            <div class="input-group-addon">万字</div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputDate" class="col-sm-2 control-label">出版日期</label>
                    <div class="col-sm-10">
                        <input type="date" class="form-control" id="InputDate" name="register_date">
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputAuthor" class="col-sm-2 control-label">作者</label>
                    <div class="col-sm-10 checkbox">
                        <div class="col-sm-12">
                            {% for author in author_all %}
                                {% if author in book2author_obj %}
                                    <input type="checkbox" id="InputAuthor" name="author_id" value="{{ author.pk }}"
                                           checked="checked">
                                    {{ author.name }}&emsp;&emsp;&emsp;
                                {% else %}
                                    <input type="checkbox" id="InputAuthor" name="author_id" value="{{ author.pk }}">
                                    {{ author.name }}&emsp;&emsp;&emsp;
                                {% endif %}
                            {% endfor %}
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="InputPublish" class="col-sm-2 control-label">出版社</label>
                    <div class="col-sm-10 radio">
                        <div class="col-sm-12">
                            {% for publish in publish_all %}
                                <input type="radio" id="InputPublish" name="publish_id" value="{{ publish.pk }}"
                                        {% if publish == book2publish_obj %} checked {% endif %}>
                                {{ publish.name }}&emsp;&emsp;&emsp;
                            {% endfor %}
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button class="btn btn-success" type="submit">提交修改</button>
                    </div>
                </div>
            </form>
        </div>

    </div>
    <script>
        $('#form_edit').submit(
            function (event) {
                event.preventDefault();
                Swal.fire({
                    title: "确定修改?",
                    text: "修改后数据将更新!",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#3085d6",
                    cancelButtonColor: "#d33",
                    confirmButtonText: "修改"
                }).then((result) => {
                    /* Read more about isConfirmed, isDenied below */
                    if (result.isConfirmed) {
                        var formData = $(this).serialize();
                        $.ajax({
                            url: '', // 根据你的需求填写正确的后端接口地址
                            type: 'post',
                            data: formData,
                            success: function (response) {
                                // 请求成功的处理
                                Swal.fire("提交成功!", "", "success").then(() => {
                                    // 在用户确认后重定向到主页
                                    window.location.href = '/book/book';
                                    {#location.reload();#}
                                })
                            },
                            error: function () {
                                // 请求失败的处理
                                Swal.fire("提交失败!", "", "error");
                            }
                        });
                    }
                });
            }
        )
    </script>
{% endblock %}